package com.sm.utils;

import com.sm.entity.Wages;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ExcelUtil {

    static public void OutputExcelByDepartment(String pathname, List<String> departments, Map<String,List<Wages>> maps){
        //文件创建
        deleteFile(pathname);
        File file = new File(pathname);
        FileOutputStream fos = null;
        // 创建文件对象wb
        XSSFWorkbook wb = null;
        try {
            fos = new FileOutputStream(file);
            wb = new XSSFWorkbook();
            //根据部门创建sheet页
            for (String department:departments){
                XSSFSheet deSheet = wb.createSheet(department);
                Row deRow = deSheet.createRow(0);
                deRow.createCell(0).setCellValue("UID");
                deRow.createCell(1).setCellValue("姓名");
                deRow.createCell(2).setCellValue("基本工资");
                deRow.createCell(3).setCellValue("职务工资");
                deRow.createCell(4).setCellValue("奖金福利");
                deRow.createCell(5).setCellValue("应发工资");
                deRow.createCell(6).setCellValue("实发工资");

                XSSFSheet wbSheet = wb.getSheet(department);
                int k = wbSheet.getLastRowNum();
                List<Wages> list = maps.get(department);
                //获取最后一行的序号
                for (int i = 1; i <= list.size(); i++) {
                    // 新建一行
                    Row row1 =wbSheet.createRow(k+i);
                    row1.createCell(0).setCellValue(list.get(i-1).getUserInfo().getUid());
                    row1.createCell(1).setCellValue(list.get(i-1).getUserInfo().getName());
                    row1.createCell(2).setCellValue(list.get(i-1).getBaseSalary());
                    row1.createCell(3).setCellValue(list.get(i-1).getJobSalary());
                    row1.createCell(4).setCellValue(list.get(i-1).getWelfare());
                    row1.createCell(5).setCellValue(list.get(i-1).getWagePayable());
                    row1.createCell(6).setCellValue(list.get(i-1).getWageReal());
                }
                wb.write(fos);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                wb.close();
                fos.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }

    static public void OutputExcel(String pathname, List<Wages> list){
        //文件创建
        deleteFile(pathname);
        File file = new File(pathname);
        FileOutputStream fos = null;
        // 创建文件对象wb
        XSSFWorkbook wb = null;
        try {
            fos = new FileOutputStream(file);
            wb = new XSSFWorkbook();
            //根据部门创建sheet页
            XSSFSheet deSheet = wb.createSheet("工资表");
            Row deRow = deSheet.createRow(0);
            deRow.createCell(0).setCellValue("UID");
            deRow.createCell(1).setCellValue("姓名");
            deRow.createCell(2).setCellValue("基本工资");
            deRow.createCell(3).setCellValue("职务工资");
            deRow.createCell(4).setCellValue("奖金福利");
            deRow.createCell(5).setCellValue("应发工资");
            deRow.createCell(6).setCellValue("实发工资");

            XSSFSheet wbSheet = wb.getSheetAt(0);
            int k = wbSheet.getLastRowNum();
            //获取最后一行的序号
            for (int i = 1; i <= list.size(); i++) {
                // 新建一行
                Row row1 =wbSheet.createRow(k+i);
                row1.createCell(0).setCellValue(list.get(i-1).getUserInfo().getUid());
                row1.createCell(1).setCellValue(list.get(i-1).getUserInfo().getName());
                row1.createCell(2).setCellValue(list.get(i-1).getBaseSalary());
                row1.createCell(3).setCellValue(list.get(i-1).getJobSalary());
                row1.createCell(4).setCellValue(list.get(i-1).getWelfare());
                row1.createCell(5).setCellValue(list.get(i-1).getWagePayable());
                row1.createCell(6).setCellValue(list.get(i-1).getWageReal());
            }
            wb.write(fos);

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                wb.close();
                fos.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }

    static private void deleteFile(String pathname){
        File file = new File(pathname);
        // 判断目录或文件是否存在
        if (file.exists()) {  // 不存在返回 false
            if (file.isFile()) {  // 为文件时调用删除文件方法
                file.delete();
            }
        }
    }
}
